package com.njitzx.service.impl;

import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import com.njitzx.constant.FileConstant;
import com.njitzx.service.FileService;
import com.njitzx.util.FileUtil;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Slf4j
public class FileServiceImpl implements FileService {
    @Autowired
    MinioClient minioClient;



    @Override
    public String upload(File tempFile) {
        String filename = tempFile.getName();
        String extension = filename.substring(filename.lastIndexOf("."));
        String objectname = FileUtil.getDefaultFolderPath() + FileUtil.getFileMd5(tempFile) + extension;
        try {
            UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder()
                    .bucket(FileConstant.bucketName).
                    object(objectname).
                    filename(tempFile.getAbsolutePath()).contentType(FileUtil.getMimeType(extension)).build();
            minioClient.uploadObject(uploadObjectArgs);
            log.info("上传成功");
            return objectname;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
